package com.aco.fatureapp.auxiliary;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aco.fatureapp.R;
import com.aco.fatureapp.model.Help;
import com.aco.fatureapp.utilitary.Utils;

public class HelpAdapter extends BaseAdapter {

	private ViewHolder mHolder;

	private final List<Help> mLstData;

	private final LayoutInflater mInflater;

	public HelpAdapter(final Context context, final List<Help> lstData) {
		// Cache the LayoutInflate to avoid asking for a new one each time.
		mInflater = LayoutInflater.from(context);

		this.mLstData = lstData;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return mLstData.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(final int index) {
		return mLstData.get(index);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(final int index) {
		return index;
	}

	static class ViewHolder {

		private TextView txtCode;

		private TextView txtDescription;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(final int position, View convertView,
			final ViewGroup viewGroup) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.adapter_help, null);
			mHolder = new ViewHolder();

			mHolder.txtCode = (TextView) convertView.findViewById(R.id.txtCode);
			mHolder.txtDescription = (TextView) convertView
					.findViewById(R.id.txtDescription);

			convertView.setTag(mHolder);
		}
		else {
			mHolder = (ViewHolder) convertView.getTag();
		}

		final Help help = mLstData.get(position);

		mHolder.txtCode.setText(Utils.formartZeroLeft(help.codigo, 2));
		mHolder.txtDescription.setText(help.descricao.toString());

		return convertView;
	}
}
